package com.ysyc.driver_ee.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.activity.DistributeOrderActivity;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseAdapter;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.tools.StatusSettingUtils;
import com.ysyc.driver_ee.tools.ToastUtils;

import io.reactivex.Observable;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/7 20:13<br>
 * 版本： v1.0<br>
 */
public class DistributeOrderAdapter extends BaseAdapter<DriverInfo> {

    private DriverInfo driverInfo;
    private NetworkCallback<Object> objectNetworkCallback;

    public DistributeOrderAdapter(Activity activity, int layoutView) {
        super(activity, layoutView);
    }

    @Override
    public void convert(MyViewHolder holder, DriverInfo item) {
        View itemView = holder.itemView;
        driverInfo = item;
        ImageView iconView = itemView.findViewById(R.id.iv_icon_view);
        TextView nameTv = itemView.findViewById(R.id.tv_name_view);
        TextView identityTv = itemView.findViewById(R.id.tv_identity_tv);
        TextView statusTv = itemView.findViewById(R.id.tv_status_view);
        TextView numberTv = itemView.findViewById(R.id.tv_number_view);
        TextView locationTv = itemView.findViewById(R.id.tv_location_view);
        StatusSettingUtils.setDriverAvatar(iconView, driverInfo.identity);

        nameTv.setText(driverInfo.driverName);
        StatusSettingUtils.setIdentity(identityTv, driverInfo.identity);
        StatusSettingUtils.setTransportStatus(statusTv, driverInfo.transportStatus);
        numberTv.setText(driverInfo.pendingOrderNum+"");
        locationTv.setText(driverInfo.driverMapName);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initNetworkCallback();
            }
        });
    }

    private void initNetworkCallback() {
        objectNetworkCallback = new NetworkCallback<>(new HttpResource<Object>() {
            @Override
            public void onSuccess(Object o) {
                ((DistributeOrderActivity)mActivity).initNetworkCallback();
                ((DistributeOrderActivity)mActivity).dismissDilog();
                ToastUtils.show("派单成功");
                mActivity.finish();
            }

            @Override
            public void onFailure(String errorDes) {
                ToastUtils.show(errorDes);
                ((DistributeOrderActivity)mActivity).dismissDilog();
            }

            @Override
            public void onLoading(Dialog dialog) {
                ((DistributeOrderActivity)mActivity).showLoadingDialog();
            }
        });
        Observable<Object> driverListObservable = RetrofitClient.getInstance().getHttpServices().distributeOrder(((DistributeOrderActivity)mActivity).orderNumber,driverInfo.id);
        objectNetworkCallback.postRequest(driverListObservable,null);
    }





}
